package logic

import (
	"context"
	"go-zero-example/cmd/userrpc/internal/dao"
	"go-zero-example/cmd/userrpc/internal/model"
	"go-zero-example/cmd/userrpc/internal/util"
	"strconv"

	"go-zero-example/cmd/userrpc/internal/svc"
	"go-zero-example/cmd/userrpc/pb/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// Register 用户注册
func (l *RegisterLogic) Register(in *userrpc.RegisterUserRequest) (*userrpc.RegisterUserResponse, error) {
	// todo: add your logic here and delete this line
	res := new(userrpc.RegisterUserResponse)
	user := new(model.User)
	user.Username = in.Username
	user.Phone = in.Phone
	userDao := new(dao.UserDao)
	err := userDao.Register(user)
	if err != nil {
		res.Message = "注册失败"
		return res, err
	}
	parseInt, err := strconv.ParseInt(util.ConvertToExternalID(user.Id), 10, 64)
	if err != nil {
		res.Message = "注册失败"
		return res, err
	}
	res.UserId = parseInt
	if err != nil {
		res.Message = "注册失败"
		return res, err
	}
	res.Message = "注册成功"
	return res, nil
}
